package com.example.myblog.dao;

import com.example.myblog.pojo.Article;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @BelongsProject: MyBlog
 * @BelongsPackage: com.example.myblog.dao
 * @ClassName ArticleDao
 * @CreateTime: 2025-05-31  21:23
 * @Author: TangLiFei
 */

@Mapper
public interface ArticleDao {
    // 查询评论数最多的 6 篇文章
    @Select("SELECT a.id, a.title, a.author, COUNT(c.id) AS commentCount " +
            "FROM t_article a " +
            "LEFT JOIN t_comment c ON a.id = c.article_id " +
            "GROUP BY a.id " +
            "ORDER BY commentCount DESC " +
            "LIMIT 6")
    List<Article> findTop6ArticlesByCommentCount();

    // 查询所有文章
    @Select("SELECT id, title, content, createTime, author FROM t_article ORDER BY createTime DESC")
    List<Article> findAll();

    // 根据ID查询文章
    @Select("select * from t_article where id=#{id}")
    public Article findById(int id);

    // 新增文章
    @Insert("insert into t_article(title,content,createTime,author)values(#{title},#{content},#{createTime},#{author})")
    public void add(Article article);

    // 修改文章
    @Update("update t_article set title=#{title},content=#{content},author=#{author} where id=#{id}")
    public void update(Article article);

    // 删除文章
    @Delete("delete from t_article where id=#{id}")
    public void delete(int id);

    // 根据条件搜索文章
    @Select("SELECT id, title, content, createTime, author FROM t_article WHERE title LIKE CONCAT('%', #{keyword}, '%') OR content LIKE CONCAT('%', #{keyword}, '%') ORDER BY createTime DESC")
    List<Article> search(String keyword);
}
